package cn.lf.android.lawfirm.home.controller;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;


public class EditionController extends BaseController {
    EditionCallback mEditionCallback;
    private Context context;

    public EditionController(EditionCallback callback, Context context) {
        this.mEditionCallback = callback;
        this.context = context;
    }

    public void onEdition() {
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("id", "1");
        new MyOkHttpClient().doGet(AppConfig.System_VersionNum, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("版本=="+response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {//myJsonObject.getString("versionNum")
                                mEditionCallback.onEditionSuccessed(myJsonObject.getString("versionNum"));
                            } else {
                                mEditionCallback
                                        .onEditionFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        Log.e("response=首页版本==", "response=首页=失败");
                        mEditionCallback.onEditionFailed();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 首页回调接口
     */
    public interface EditionCallback {
        /**
         * 首页请求成功
         */
        public void onEditionSuccessed(String beans);

        /**
         * 首页请求失败
         */
        public void onEditionFailed();

    }
}
